package com.aacar.op.web;

import com.aacar.op.utils.DefineAppConfig;
import com.aacar.op.utils.mapper.JsonMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;

/**
 * 容器初始化设置的ServletContext级别的变量
 *
 * @author: Gongle
 */
public class SystemContextAware implements ServletContextAware {
    private static Logger logger = LoggerFactory.getLogger(SystemContextAware.class);

    private ServletContext servletContext;

    public void init() {

        logger.debug("写入ServletContext级别变量.");

        String ctx = getServletContext().getContextPath();

        //ServletContext 级别的变量
        DefineAppConfig.ctx = ctx;
        this.servletContext.setAttribute("ctx", DefineAppConfig.ctx);
        this.servletContext.setAttribute("upload_host", DefineAppConfig.static_host);

        JsonMapper jsonMapper = JsonMapper.alwaysMapper();
        String configJsonStr = jsonMapper.toJson(DefineAppConfig.asMap());
        this.servletContext.setAttribute("P", configJsonStr);
        this.servletContext.setAttribute("DefineAppConfig", configJsonStr);
    }


    public ServletContext getServletContext() {
        return servletContext;
    }

    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }


}
